const PatternStripTemplate = [
  [
    [[0, 0, 0, 0, 0], [0, 0, 4, 4, 4]],
    [[1, 1, 1, 1, 1], [1, 1, 2, 2, 2]],
    [[2, 2, 2, 2, 2], [0, 0, 0, 2, 2]],
    [[3, 3, 3, 3, 3], [1, 1, 1, 3, 3]],
    [[4, 4, 4, 4, 4], [3, 3, 3, 4, 4]],
    [[0, 0, 0, 0, 3], [2, 3, 3, 3, 3]],
    [[0, 1, 1, 1, 1], [1, 4, 4, 4, 4]],
    [[2, 2, 2, 2, 4], [0, 1, 3, 5, 5]]
  ],
  [
    [[0, 0, 0, 0, 0], [0, 0, 4, 4, 4]],
    [[1, 1, 1, 1, 1], [1, 1, 2, 2, 2]],
    [[2, 2, 2, 2, 2], [0, 0, 0, 2, 2]],
    [[3, 3, 3, 3, 3], [1, 1, 1, 3, 3]],
    [[4, 4, 4, 4, 4], [3, 3, 3, 4, 4]],
    [[0, 0, 0, 0, 3], [0, 1, 1, 1, 1]],
    [[2, 2, 2, 2, 4], [2, 3, 3, 3, 3]],
    [[1, 4, 4, 4, 4], [0, 2, 3, 5, 5]]
  ],
  [
    [[0, 0, 0, 0, 0], [0, 0, 4, 4, 4]],
    [[1, 1, 1, 1, 1], [1, 1, 2, 2, 2]],
    [[2, 2, 2, 2, 2], [0, 0, 0, 2, 2]],
    [[3, 3, 3, 3, 3], [1, 1, 1, 3, 3]],
    [[4, 4, 4, 4, 4], [3, 3, 3, 4, 4]],
    [[2, 2, 2, 2, 4], [2, 3, 3, 3, 3]],
    [[0, 1, 1, 1, 1], [1, 4, 4, 4, 4]],
    [[0, 0, 0, 0, 3], [1, 2, 4, 5, 5]]
  ],
  [
    [[0, 0, 0, 0, 0], [0, 0, 4, 4, 4]],
    [[1, 1, 1, 1, 1], [1, 1, 2, 2, 2]],
    [[2, 2, 2, 2, 2], [0, 0, 0, 2, 2]],
    [[3, 3, 3, 3, 3], [1, 1, 1, 3, 3]],
    [[4, 4, 4, 4, 4], [3, 3, 3, 4, 4]],
    [[0, 0, 0, 0, 3], [2, 3, 3, 3, 3]],
    [[1, 4, 4, 4, 4], [2, 2, 2, 2, 4]],
    [[0, 1, 1, 1, 1], [2, 3, 4, 5, 5]]
  ]
]

// const PaneTemplate = [
//   { color: '灰', done: false },
//   { color: '银', done: false },
//   { color: '青', done: false },
//   { color: '粉', done: false },
//   { color: '橙', done: false },
//   { color: '黄', done: false },
// ]

const PaneTemplate = [
  { color: 0, done: false }, // 银
  { color: 1, done: false }, // 青
  { color: 2, done: false }, // 粉
  { color: 3, done: false }, // 橙
  { color: 4, done: false }, // 黄
  { color: 5, done: false }, // 灰，百搭
]

const PATTERN_STRIP_COUNT = 8
const PATTERN_STRIP_PER_SIDE = 5
const MAX_ROUND = 6
const MAX_DECUCT_LEVEL = 10

/**
 * 针对单个宫殿洗牌
 */
function shufflePatternStrip(source) {
  let a = deepcopy(source)
  // 随机交换正反面，最后一列包含百搭元素，百搭元素默认在反面
  for (let i = 0; i < PATTERN_STRIP_COUNT - 1; i++) {
    let r = Math.random()
    if (r > 0.5) {
      let tmp = a[i][1]
      a[i][1] = a[i][0]
      a[i][0] = tmp
    }
  }
  // 随机交换顺序
  for (let i = 0; i < PATTERN_STRIP_COUNT; i++) {
    let r = parseInt(Math.random() * 8, 10)
    let tmp = a[r]
    a[r] = a[i]
    a[i] = tmp
  }
  return a
}

/**
 * 针对单个宫殿补充信息
 */
function addInfo(source) {
  for (let i = 0; i < PATTERN_STRIP_COUNT; i++) {
    for (let j = 0; j < PATTERN_STRIP_PER_SIDE; j++) {
      source[i][0][j] = Object.create(PaneTemplate[source[i][0][j]])
      source[i][1][j] = Object.create(PaneTemplate[source[i][1][j]])
    }
  }
  return source
}

function createPalaces(num) {
  let result = []
  for (let i = 0; i < num; i++) {
    // 洗牌
    let p = shufflePatternStrip(PatternStripTemplate[i])
    // 将数字替换为颜色和状态
    addInfo(p)
    result.push(p)
  }
  return result
}

/**
 * 多维数组深度克隆
 * @param {array} obj 
 */
function deepcopy(obj) {
  var out = [],i = 0,len = obj.length;
  for (; i < len; i++) {
      if (obj[i] instanceof Array){
          out[i] = deepcopy(obj[i]);
      }
      else out[i] = obj[i];
  }
  return out;
}

export {
  createPalaces,
  PATTERN_STRIP_COUNT,
  MAX_ROUND,
  MAX_DECUCT_LEVEL
}